import configparser


class ConfRead(object):
    def __init__(self):
        self.config = configparser.ConfigParser()
        self.config.read('C:\\Users\\Administrator\\Desktop\\陈飞\\主机批量管理\\conf\\host_manage.conf')

    def show_group(self):
        group_dict = {}
        for i in self.config.sections():
            if self.config[i]['group'] in group_dict.keys():
                group_dict[self.config[i]['group']][i]= self.config[i]['ip']
            else:
                group_dict[self.config[i]['group']] = {i: self.config[i]['ip']}

        return group_dict

    def show_host(self, group):
        host_dict = {}
        for i in self.config.sections():
            if self.config[i]['group'] == group:
                host_dict[i] = self.config[i]['ip']

        return host_dict

    def show_all_host(self):
        allhost_dict = {}
        for i in self.config.sections():
            allhost_dict[i] = self.config[i]['ip']

        return allhost_dict

    def about_group(self, group):
        group_info = []
        for i in self.config.sections():
            if self.config[i]['group'] == group:
                group_info.append(
                    (self.config[i]['ip'], int(self.config[i]['port']), self.config[i]['user'], self.config[i]['pwd'])
                )

        return group_info

    def about_host(self, hosts):
        host_info = []
        for h1 in hosts:
            for i in self.config.sections():
                if h1 == i:
                    host_info.append(
                        (self.config[i]['ip'], int(self.config[i]['port']), self.config[i]['user'], self.config[i]['pwd'])
                    )

        return host_info